package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.OperationV2Rule;
import com.novax.ex.activity.infrastructure.entity.OperationV2SubRule;
import com.novax.ex.activity.open.api.inner.OperationV2SubRuleApi;
import com.novax.ex.activity.open.model.request.OperationV2SubRuleRequest;
import com.novax.ex.activity.open.model.response.OperationV2SubRuleResponse;
import com.novax.ex.activity.open.model.response.OperationV2RuleResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.OperationV2SubRuleService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * @Description 活动落地页配置V2
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationV2SubRuleController extends BaseActivityController implements OperationV2SubRuleApi {

    @Resource
    private OperationV2SubRuleService operationV2SubRuleService;

    @Override
    public ReturnResult<List<OperationV2SubRuleResponse>> list(Long operationSubId) {
        return ReturnResult.success(CopyUtils.copyList(operationV2SubRuleService.list(operationSubId), OperationV2SubRuleResponse.class));
    }

    @Override
    public ReturnResult<?> add(String languages, OperationV2SubRuleRequest operationV2SubRuleRequest,
                               BindingResult bindingResult) {

        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LACK_VALUE));
        }
        // 判断此活动id和语言是否已配置文案
        Long count = operationV2SubRuleService.queryOperationIdAndLanguage(operationV2SubRuleRequest.getOperationSubId(),
                                                                           operationV2SubRuleRequest.getLanguage());
        if(count >= 1){
            return ReturnResult.fail(getInternational(languages, InternationalCode.DUPLICATED_DATA));
        }

        OperationV2SubRule operationV2SubRule = CopyUtils.copyObject(operationV2SubRuleRequest, OperationV2SubRule.class);

        boolean res = operationV2SubRuleService.addSelective(operationV2SubRule);
        return res ? ReturnResult.success(getInternational(languages, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(languages, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> modify(String languages, Long id, OperationV2SubRuleRequest operationV2SubRuleRequest,
                                  BindingResult bindingResult) {

        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LACK_VALUE));
        }

        OperationV2SubRule current = operationV2SubRuleService.find(id);
        // 语言不可更改
        if(!current.getLanguage().equals(operationV2SubRuleRequest.getLanguage())){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LANGUAGE_CHANG));
        }

        OperationV2SubRule operationV2SubRule = CopyUtils.copyObject(operationV2SubRuleRequest, OperationV2SubRule.class);
        operationV2SubRule.setId(id);
        operationV2SubRule.setOperationSubId(null);
        operationV2SubRule.setLanguage(null);
        boolean res = operationV2SubRuleService.modifySelective(operationV2SubRule);
        return res ? ReturnResult.success(getInternational(languages, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(languages, InternationalCode.OPERATION_ERROR));
    }

}
